package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @author 刘付磊
 * @date 2021/11/12 0012
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {userInfoMapper.insert(userInfo);}

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper();
        lqw.in(UserInfo::getId,userIds);
        //2.添加筛选条件
        if(userInfo != null) {
            if(userInfo.getAge() != null) {
                lqw.lt(UserInfo::getAge,userInfo.getAge());
            }
            if(!StringUtils.isEmpty(userInfo.getGender())) {
                lqw.eq(UserInfo::getGender,userInfo.getGender());
            }
            if(!StringUtils.isEmpty(userInfo.getNickname())) {
                lqw.like(UserInfo::getNickname,userInfo.getNickname());
            }

        }
        List<UserInfo> userInfos = userInfoMapper.selectList(lqw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(userInfos, "id");
        return map;
    }
}
